package com.laivi.basic.util;

import java.math.BigDecimal;
import java.util.Locale;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.laivi.basic.common.constants.Constant;
import com.laivi.basic.model.algorithm.stack.ArrayStack;
import com.laivi.basic.model.algorithm.stack.Stack;

/**
 * @author laivi.zhu@gmail.com
 * @date 2014-6-6
 * description:
 */
public class DataUtil {

	public static final Pattern pat=Pattern.compile("[\u4e00-\u9fa5]");
	
	public static boolean isEmpty(String value){
		if(value!=null && !"".equals(value) && !value.equalsIgnoreCase("null")){
			return false;
		}else{
			return true;
		}
	}
	
	public static boolean isNull(Object obj){
		if(obj==null) return true;
		if(obj instanceof String) return DataUtil.isEmpty((String)obj);
		return false;
	}
	
	public static String emptyBack(Object obj){
		if(DataUtil.isNull(obj)) return Constant.EMPTY;
		return String.valueOf(obj);
	}
	
	public static double format(double value,int scale){
		BigDecimal bd = new BigDecimal(value);
		bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return Double.parseDouble(bd.toString());
	}
	
	/**
	 * 将浮点数转换成百分比
	 * @date 2013-12-17
	 * @param value
	 * @param scale
	 * @return
	 */
	public static String getPercent(double value, int scale) {
		if(value==1){
			return "100%";
		}
		return DataUtil.format(value*100, scale)+"%";
	}
	
	/**
	 * 格式化特定位数的整型变量，位数不足补零
	 * @date 2014-6-22
	 * @param value
	 * @param scale
	 * @return
	 */
	public static String format(int value,int scale){
		String valueStr=String.valueOf(value);
		StringBuffer sb=new StringBuffer();
		if(valueStr.length()<scale){
			for(int i=0;i<scale-valueStr.length();i++){
				sb.append("0");
			}
		}
		sb.append(valueStr);
		return sb.toString();
	}
	
	/**
	 * 中文信息获取
	 * @date 2013-8-2
	 * @param key
	 * @return
	 */
	public static String getI18NMsg(String key){
        return DataUtil.getI18NMsg(key,Locale.CHINA);
    }

	/**
	 * 国际化信息获取
	 * @date 2013-8-2
	 * @param key
	 * @param locale
	 * @return
	 */
    public static String getI18NMsg(String key,Locale locale){
        return ResourceBundle.getBundle("projectMessage",locale).getString(key);
    }
    
    /**
	 * 获取特定长度的随机字符串
	 * @date 2013-8-11
	 * @param length
	 * @return
	 */
	public static String getRandPassword(int length) {
		String passwordChar = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*_";
		Random rand = new Random();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			sb.append(passwordChar.charAt(rand.nextInt(passwordChar.length())));
		}
		return sb.toString();
	}
	
	/**
	 * 是否包含中文
	 * @date 2013-8-29
	 * @param str
	 * @return
	 */
	public static boolean isContainChinese(String str){
		Matcher matcher = pat.matcher(str);
		if(matcher.find()){
			return true;
		}
		return false;
	}
	
	/**
     * 将字符串首字母大写
     * @param value 字符串
     * @return 首字母大写后的新字符串
     */
	public static String capitalize(String value){
		if (null == value)
            return null;
        int len = value.length();
        if (len == 0)
            return "";
        char char0 = value.charAt(0);
        if (Character.isUpperCase(char0))
            return value.toString();
        return new StringBuilder(len).append(Character.toUpperCase(char0))
                                     .append(value.subSequence(1, len))
                                     .toString();
	}
	
	@SuppressWarnings("unchecked")
	public static <T>T nullValue(Object object,T value){
		if(object==null){
			return value;
		}else{
			return (T)object;
		}
	}
	
	public static String convert(int num,int b) {
		Stack<Integer> stack=new ArrayStack<Integer>();
		while(num!=0){
			stack.push(num%b);
			num=num/b;
		}
		int maxcount=stack.getCurrentCount();
		if(maxcount==0){
			return "0";
		}else{
			StringBuilder result=new StringBuilder();
			for(int i=0;i<maxcount;i++){
				result.append(renderResult(stack.pop(),b));
			}
			return result.toString();
		}
	}
	
	private static String renderResult(int num,int b){
		String result=null;
		if(b==16){
			char[] character={'A','B','C','D','E','F'};
			for(int i=0;i<character.length;i++){
				if(num==(i+10)){
					result= character[i]+Constant.EMPTY;
					break;
				}
			}
		}
		return  DataUtil.nullValue(result,num+Constant.EMPTY);
	}
    
}
